package com.jerry.huihome.module.find_near_mine.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioGroup;

import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by axiang on 2016/5/10.
 */

public class Find_Fragment extends BaseFragment{

    @BindView(R.id.rg_find)
    RadioGroup rg_Find;
    @BindView(R.id.vp_find)
    ViewPager vp_Find;
    private List<Fragment> fragmentList;
    private Find_AfflatusFragment find_afflatusFragment;
    private Find_ActiviytFragment find_activiytFragment;
    private String tag;

    public Find_Fragment(){
    }

    @Override
    protected void initViews(){
        fragmentList = new ArrayList<>();
        /*
        error:
        Process: com.jerry.template, PID: 19807
        android.content.res.Resources$NotFoundException: Resource ID #0x0
        1.当你int aaa ,setText(aaa),你就错了-->>aaa+"";
        2.当你只完成一个Find_AfflatusFragment,而Find_ActiviytFragment只是创建了,而没有bindview布局时
        你就错了
        */
        find_afflatusFragment = BaseFragment.newInstance(Find_AfflatusFragment.class, null);
        find_activiytFragment = BaseFragment.newInstance(Find_ActiviytFragment.class, null);
        fragmentList.add(find_afflatusFragment);
        fragmentList.add(find_activiytFragment);
        //vp_Find.setAdapter(new FragmentPagerAdapter(this.getActivity().getSupportFragmentManager()){
        vp_Find.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()){

            @Override
            public int getCount(){
                return fragmentList.size();
            }

            @Override
            public Fragment getItem(int position){
                return fragmentList.get(position);
            }
        });
        vp_Find.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels){
            }

            @Override
            public void onPageSelected(int position){
                View btn = rg_Find.getChildAt(position);
                rg_Find.check(btn.getId());
            }

            @Override
            public void onPageScrollStateChanged(int state){
            }
        });
        rg_Find.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId){
                int childCount = group.getChildCount();
                for(int i = 0; i < childCount; i++){
                    if(group.getChildAt(i).getId() == checkedId){
                        vp_Find.setCurrentItem(i);
                        break;
                    }
                }
            }
        });
        if(tag == "activity"){
            vp_Find.setCurrentItem(1);
        }
    }

    /*@Override
    protected void postEventResult(@NonNull EventMessage message){
        String type = message.type;
        if(type == "activity"){
            vp_Find.setCurrentItem(1);
        }
    }*/

    @Override
    protected void getReqParams(Bundle bundle){
        tag = bundle.getString("tag");
    }

    @Override
    protected int getLayoutId(){
        return R.layout.find;
    }
}
